Skip to content

Use chrono to detect local timezone thread-safely #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

Techcable
Copy link
Member

@Techcable Techcable commented Feb 18, 2024

As discussed in PR #44, the 'time' crate fails timezone detection
in the presence of multiple threads, because the underlying
POSIX function localtime_r is not thread safe.

In order to avoid these thread-safety issues, we use the chrono crate.
It avoids system libraries and reimplements timezone detection from scratch.
Thanks to @yaozongyou for suggesting this fix.

There are two improvements that could be made to reduce dependency bloat:

  • Only enable this on unix systems
  • Switch to chono entirely.
    However, this fix is sufficient to avoid the bug and that is the priority.

It is possible that the optimizer is smart enough to remove all chrono code
except the localtime detection functionality.
In that case, switching from time to chrono could increase bloat
by pulling in two copies of time formatting code.
Tests/benchmarks are needed to determine the best approach.

This superceeds PR #44.

@Techcable Techcable force-pushed the fix/localtime-thread-safety branch from 4465f02 to 4c37f20 Compare August 20, 2025 22:54
@Techcable
Copy link
Member Author

Techcable commented Aug 20, 2025

The time crate now has a MSRV of 1.81, which seems to break the CI. It appears chrono has a far more conservative MSRV of 1.62. This is sufficient reason to switch.

@Techcable Techcable force-pushed the fix/localtime-thread-safety branch 2 times, most recently from 70cdb68 to d2df947 Compare August 21, 2025 00:19
As discussed in PR slog-rs#44, the 'time' crate fails timezone detection
in the presence of multiple threads, because the underlying
POSIX function localtime_r is not thread safe.

In order to avoid these thread-safety issues, we use the chrono crate.
It avoids system libraries and reimplements timezone detection from scratch.
Thanks to @yaozongyou for suggesting this fix.

There are two improvements that could be made to reduce dependency bloat:
- Only enable this on unix systems
- Switch to chono entirely.
However, this fix is sufficient to avoid the bug and that is the priority.

It is possible that the optimizer is smart enough to remove all chrono code
except the localtime detection functionality.
In that case, switching from time to chrono could increase bloat
by pulling in two copies of time formatting code.
Tests/benchmarks are needed to determine the best approach.
@Techcable Techcable force-pushed the fix/localtime-thread-safety branch from d2df947 to 42630e4 Compare August 21, 2025 00:22
@Techcable Techcable marked this pull request as ready for review August 21, 2025 00:25
@Techcable
Copy link
Member Author

This is important enough to warrant a patch release. It should not wait for 2.10.

@Techcable
Copy link
Member Author

Merged in 835561b.

@Techcable Techcable closed this Aug 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant